﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Cors.Infrastructure;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Smart_Archives_Api.DTO;
using Smart_Archives_Api.Entity;

namespace Smart_Archives_Api.Controllers
{
    /// <summary>
    /// 大屏接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [EnableCors("AllowAllOrigin")]
    public class ScreenController : ControllerBase
    {
        private readonly SaContext saContext;

        public ScreenController(SaContext saContext)
        {
            this.saContext = saContext;
        }

        /// <summary>
        /// 获取设备分组
        /// </summary>
        /// <returns></returns>
        [HttpGet(Name = "GetDeviceCategory")]
        public Response<DeviceCategoryDto> GetDeviceCategory()
        {
            var res = new Response<DeviceCategoryDto>();
            DeviceCategoryDto deviceCategory = new DeviceCategoryDto();
            var parentCategories = new List<DeviceCategory>();
            //获取根分类
            var parentCategoryList = saContext.SecurityDeviceCategory.Where(a => a.ParentId == 0 && a.Status == 1).ToList();
            foreach (var parentItem in parentCategoryList)
            {

                var model = new DeviceCategory();
                model.Id = parentItem.Id;
                model.Name = parentItem.Name;
                model.Type = (int)parentItem.Type;
                model.CreateTime = parentItem.CreateTime?.ToString("yyyy-MM-dd hh:mm");
                var childCategories = new List<DeviceCategory>();
                //获取分类
                var childCategoryList = saContext.SecurityDeviceCategory
                    .Where(a => a.ParentId == parentItem.Id && a.Status == 1).ToList();
                foreach (var childItem in childCategoryList)
                {
                    var item = new DeviceCategory();
                    item.Id = childItem.Id;
                    item.Name = childItem.Name;
                    item.Type = (int)childItem.Type;
                    item.CreateTime = childItem.CreateTime?.ToString("yyyy-MM-dd hh:mm");
                    childCategories.Add(item);
                }

                model.ChildCategories = childCategories;
                parentCategories.Add(model);
            }

            deviceCategory.ParentCategories = parentCategories;
            res.Data = deviceCategory;
            res.Code = 1;
            res.Msg = "ok";
            return res;
        }


        /// <summary>
        /// 获取设备列表
        /// </summary>
        /// <param name="categoryId">设备分类id</param>
        /// <returns></returns>
        [HttpGet]
        public Response<List<SecurityDevice>> GetDeviceList(int categoryId)
        {
            var res = new Response<List<SecurityDevice>>();
            var deviceList = saContext.SecurityDevice.Where(a => a.SecurityCategoryId == categoryId && a.Status == 1)
                .OrderByDescending(a => a.CreateTime).ToList();
            res.Data = deviceList;
            res.Code = 1;
            res.Msg = "ok";
            return res;
        }


        /// <summary>
        /// 获取设备记录
        /// </summary>
        /// <param name="deviceId">id</param>
        /// <param name="startDate">记录起始日期(string类型起始结束为空获取所有事件记录)</param>
        /// <param name="endDate">记录结束日期(string类型可起始结束为空获取所有事件记录)</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页条数</param>
        /// <returns></returns>
        [HttpGet]
        public Response<DeviceRecordsDto> GetDeviceRecord(int deviceId, string startDate,string endDate, int pageIndex, int pageSize)
        {
            var res = new Response<DeviceRecordsDto>();
            var dto=new DeviceRecordsDto();
            List<SecurityDeviceRecord> record;
            //判断日期是否为空
            if (!string.IsNullOrEmpty(startDate)&&!string.IsNullOrEmpty(endDate))
            {
                record = saContext.SecurityDeviceRecord
                    .Where(a => a.SecurityDeviceId == deviceId && a.CreateTime.Value.Date >= Convert.ToDateTime(startDate).Date&& a.CreateTime.Value.Date <= Convert.ToDateTime(endDate).Date)
                    .OrderByDescending(a => a.CreateTime).ToList();
            }
            else if (string.IsNullOrEmpty(startDate) && string.IsNullOrEmpty(endDate))
            {
                record = saContext.SecurityDeviceRecord
                    .Where(a => a.SecurityDeviceId == deviceId)
                    .OrderByDescending(a => a.CreateTime).ToList();
            }
            else
            {
                res.Code = 0;
                res.Msg = "请求参数不正确";
                return res;
            }

            //分页
            var list = record.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            dto.SecurityDeviceRecords = list;
            dto.TotalCount = record.Count;
            res.Data = dto;
            res.Code = 1;
            res.Msg = "ok";
            return res;
        }

        /// <summary>
        /// 获取设备记录所有日期
        /// </summary>
        /// <param name="deviceId">设备id</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns></returns>
        [HttpGet]
        public Response<List<string>> GetRecordDate(int deviceId, int pageIndex, int pageSize)
        {
            var res = new Response<List<string>>();
            List<string> record;
            //判断日期是否为空
            record = saContext.SecurityDeviceRecord.Where(a => a.SecurityDeviceId == deviceId).Select(a => a.EventTime.Value.ToString("yyyy-MM-dd"))
                .Distinct().ToList();
            //分页
            var list = record.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            res.Data = list;
            res.Code = 1;
            res.Msg = "ok";
            return res;
        }

        /// <summary>
        /// 获取日志
        /// </summary>
        /// <param name="logType">日志类型 更新 新增 删除 报表 错误</param>
        /// <param name="dateTime">记录日期 没有传空</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页条数</param>
        /// <returns></returns>
        //[HttpGet]
        //public Response<List<TLog>> GetLogs(string logType, string dateTime, int pageIndex, int pageSize)
        //{
        //    var res=new Response<List<TLog>>();
        //    var logs = saContext.TLog.Where(a => a.LogType == logType).ToList();
        //    if (!string.IsNullOrEmpty(dateTime))
        //    {
        //        logs = logs.Where(a => a.LogTime.Date == Convert.ToDateTime(dateTime).Date).ToList();
        //    }
        //    var list=logs.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
        //    res.Data = list;
        //    res.Code = 1;
        //    res.Msg = "ok";
        //    return res;
        //}
    }
}